package com.spring.sunflower.main.presenter;

import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;
import com.spring.sunflower.base.BaseObserver;
import com.spring.sunflower.base.BasePresenter;
import com.spring.sunflower.bean.BannerBean;
import com.spring.sunflower.bean.BlackHasBean;
import com.spring.sunflower.bean.LiveApplyChatBean;
import com.spring.sunflower.bean.RecommendAnchorsBean;
import com.spring.sunflower.constant.Constant;
import com.spring.sunflower.main.MainContract;

import io.reactivex.rxjava3.core.Observable;

/**
 * Created by Kevin on 2021/6/1<br/>
 * <p>
 * Describe:<br/>
 */
public class LivelyPresenter extends BasePresenter<MainContract.LivelyView> {
    private String TAG = "MainPresenter";

    public LivelyPresenter(MainContract.LivelyView baseView) {
        super(baseView);
    }


    public void getRecommendAnchorsFruit(int pageIndex) {
        Observable<RecommendAnchorsBean> observable = requestDataSource.requestRecommendAnchorsFruit(pageIndex);
        addDisposable(observable, new BaseObserver<RecommendAnchorsBean>(baseView) {

            @Override
            public void onSuccess(RecommendAnchorsBean bean) {
                String json = new Gson().toJson(bean);
                LogUtils.dTag(TAG, "getRecommendAnchors=" + json);
//                RecommendAnchorsBean recommendAnchorsBean = (RecommendAnchorsBean) handleJson(o, RecommendAnchorsBean.class);
                String status = bean.getStatus();
                baseView.hideLoading();
//                if (status == 1) {
                baseView.showAnchorData(bean.getDatas());
//                } else {

//                }
            }

            @Override
            public void onError(int status, String msg) {

            }
        });
    }
    public void getRecommendAnchors(int pageIndex) {
        getRecommendAnchors(pageIndex, "");
    }

    public void getRecommendAnchors(int pageIndex, String labelId) {
        Observable<RecommendAnchorsBean> observable = requestDataSource.requestRecommendAnchors(pageIndex, labelId);
        addDisposable(observable, new BaseObserver<RecommendAnchorsBean>() {

            @Override
            public void onSuccess(RecommendAnchorsBean bean) {
                String json = new Gson().toJson(bean);
                LogUtils.dTag(TAG, "getRecommendAnchors=" + json);
                baseView.hideLoading();
                baseView.showAnchorData(bean.getDatas());
            }

            @Override
            public void onError(int status, String msg) {

            }
        });
    }

    public void getBanner() {
        Observable<BannerBean> observable = requestDataSource.requestBanner();
        addDisposable(observable, new BaseObserver<BannerBean>() {

            @Override
            public void onSuccess(BannerBean bean) {
                String json = new Gson().toJson(bean);
                LogUtils.dTag(TAG, "getBanner=" + json);

//                if (bannerBean.getStatus() == 1) {
                baseView.showBannerData(bean.getDatas());
//                }else {
//
//                }

            }

            @Override
            public void onError(int status, String msg) {
                baseView.showError(msg);
            }
        });
    }
    /**
     * @param userId   本人id
     * @param anchorId 对方id
     */
    public void applyChat(String userId, String anchorId) {
        String gender = getField(Constant.KEY_GENDER, "");
        Observable<LiveApplyChatBean> observable;
        if ("1".equals(gender)) {
            observable = requestDataSource.liveApplyChat(userId, anchorId);
        } else {
            observable = requestDataSource.liveApplyChatAnchor(anchorId);
        }
        addDisposable(observable, new BaseObserver<LiveApplyChatBean>(baseView) {
            @Override
            public void onSuccess(LiveApplyChatBean bean) {
                baseView.liveApplySuccess();
            }

            @Override
            public void onError(int status, String msg) {

            }
        });
    }

    public void sayHello(String anchorId) {
        Observable<BlackHasBean> observable = requestDataSource.sayHello(anchorId);
        addDisposable(observable, new BaseObserver<BlackHasBean>(baseView) {

            @Override
            public void onSuccess(BlackHasBean bean) {
//                baseView.hideLoading();
                baseView.sayHelloSuccess();
            }

            @Override
            public void onError(int status, String msg) {
//                baseView.sayHelloFailed(msg);
            }
        });
    }
}
